using Oracle.ManagedDataAccess.Client;

namespace OracleInternal.BinXml
{
	internal class ObxmlDecodeContext
	{
		protected bool m_IsDone;

		protected ObxmlMetaDataRepository m_MetaDataRepository;

		protected ObxmlTokenManagerContext m_TokenMapContext;

		protected ObxmlTokenMap m_TokenMap;

		protected OracleConnection m_DbConnection;

		protected long m_SpecificTokenId;

		internal const int DEFAULT_XMLTYPE_NODEPOOL_SIZE = 10000;

		internal static int m_XMLTypeNodePoolSize = 10000;

		internal static bool m_XMLTypeOpcodeDump = false;

		internal static bool m_XMLTypeParseAllXml = false;

		internal static bool m_XMLTypeParseXml = false;

		internal static bool m_XMLTypeUseHeaderEncodingFromServer = false;

		internal const int DEFAULT_XMLTYPE_OPTIMIZATION_LEVEL = 0;

		internal const int XMLTYPE_OPTIMIZATION_LEVEL_NO_CHAR_REPLACEMENT = 1;

		internal static int m_XMLTypeOptimizationLevel = 0;

		internal const int DEFAULT_XMLTYPE_MAX_CACHE_ENTRIES = 70000;

		internal static int m_XMLTypeMaxCacheEntries = 70000;

		internal CsxDecodeChunkingPolicy ChunkingPolicy;

		internal long CsxReadMaxChunkSize;

		protected string DecodeTimeStamp => string.Empty;

		protected string CacheSizeString => string.Empty;

		protected string PerformanceCounterString => string.Empty;

		internal string ContextId => string.Empty;

		internal bool IsBusy => !m_IsDone;

		internal bool IsValid
		{
			get
			{
				if (DbConnection != null && TokenMap != null)
				{
					return m_MetaDataRepository != null;
				}
				return false;
			}
		}

		internal OracleConnection DbConnection
		{
			get
			{
				return m_DbConnection;
			}
			set
			{
				if (m_IsDone)
				{
					m_DbConnection = value;
				}
			}
		}

		internal ObxmlTokenManagerContext TokenMapContext
		{
			get
			{
				return m_TokenMapContext;
			}
			set
			{
				m_TokenMapContext = value;
			}
		}

		internal ObxmlTokenMap TokenMap
		{
			get
			{
				if (m_TokenMap == null && m_TokenMapContext != null && DbConnection != null)
				{
					m_TokenMap = ObxmlTokenManager.GetOracleBinXmlTokenManager(DbConnection).Open(m_TokenMapContext);
				}
				return m_TokenMap;
			}
			set
			{
				m_TokenMap = value;
			}
		}

		internal ObxmlMetaDataRepository MetaDataRepository => m_MetaDataRepository;

		protected ObxmlDecodeContext()
		{
			CsxReadMaxChunkSize = ObxmlDecodeStream.DefaultChunkSize;
			ChunkingPolicy = CsxDecodeChunkingPolicy.None;
		}

		internal static ObxmlDecodeContext CreateDefaultContext(OracleConnection dbConnection)
		{
			ObxmlDecodeContext obxmlDecodeContext = new ObxmlDecodeContext();
			obxmlDecodeContext.SetDecodeContext(dbConnection, null, ObxmlTokenManagerContext.CreateDefaultTokenManagerContext(dbConnection), CsxDecodeChunkingPolicy.None, 0L);
			return obxmlDecodeContext;
		}

		internal static ObxmlDecodeContext UpdateContext(ObxmlDecodeContext decodeContext, ObxmlMetaDataRepository metaDataRepository, ObxmlTokenMap tokenMap, CsxDecodeChunkingPolicy chunkingPolicy = CsxDecodeChunkingPolicy.None, long csxReadMaxChunkSize = -1L)
		{
			decodeContext.SetDecodeContext(null, metaDataRepository, tokenMap, chunkingPolicy, csxReadMaxChunkSize);
			return decodeContext;
		}

		protected string GetTimeStamp(bool isStart)
		{
			return string.Empty;
		}

		internal void SetDecodeContext(ObxmlDecodeContext decodeContext)
		{
			if (decodeContext != null && this != decodeContext)
			{
				if (decodeContext.TokenMap != null)
				{
					TokenMap = decodeContext.TokenMap;
				}
				if (decodeContext.DbConnection != null)
				{
					m_DbConnection = decodeContext.DbConnection;
				}
				if (decodeContext.MetaDataRepository != null)
				{
					m_MetaDataRepository = decodeContext.MetaDataRepository;
				}
				if (CsxDecodeChunkingPolicy.None != decodeContext.ChunkingPolicy)
				{
					ChunkingPolicy = decodeContext.ChunkingPolicy;
				}
				if (decodeContext.CsxReadMaxChunkSize > 0)
				{
					CsxReadMaxChunkSize = decodeContext.CsxReadMaxChunkSize;
				}
			}
		}

		protected ObxmlDecodeContext SetDecodeContext(OracleConnection dbConnection, ObxmlMetaDataRepository metaDataRepository, ObxmlTokenMap tokenMap, CsxDecodeChunkingPolicy chunkingPolicy, long csxReadMaxChunkSize)
		{
			if (dbConnection != null)
			{
				m_DbConnection = dbConnection;
			}
			if (metaDataRepository != null)
			{
				m_MetaDataRepository = metaDataRepository;
			}
			if (tokenMap != null)
			{
				TokenMap = tokenMap;
			}
			if (CsxDecodeChunkingPolicy.None != chunkingPolicy)
			{
				ChunkingPolicy = chunkingPolicy;
			}
			if (csxReadMaxChunkSize > 0)
			{
				CsxReadMaxChunkSize = csxReadMaxChunkSize;
			}
			return this;
		}

		protected ObxmlDecodeContext SetDecodeContext(OracleConnection dbConnection, ObxmlMetaDataRepository metaDataRepository, ObxmlTokenManagerContext tokenMapContext, CsxDecodeChunkingPolicy chunkingPolicy, long csxReadMaxChunkSize)
		{
			if (dbConnection != null)
			{
				m_DbConnection = dbConnection;
			}
			if (metaDataRepository != null)
			{
				m_MetaDataRepository = metaDataRepository;
			}
			if (tokenMapContext != null)
			{
				TokenMapContext = tokenMapContext;
			}
			if (CsxDecodeChunkingPolicy.None != chunkingPolicy)
			{
				ChunkingPolicy = chunkingPolicy;
			}
			if (csxReadMaxChunkSize > 0)
			{
				CsxReadMaxChunkSize = csxReadMaxChunkSize;
			}
			return this;
		}

		internal virtual bool ResetDecodeState(bool disposing)
		{
			CsxReadMaxChunkSize = ObxmlDecodeStream.DefaultChunkSize;
			ChunkingPolicy = CsxDecodeChunkingPolicy.None;
			return true;
		}

		internal static int GetOutputLengthEstimate(int inputCSXLength, bool fullBufferEstimate = false)
		{
			if (fullBufferEstimate)
			{
				return inputCSXLength * 3 / 2;
			}
			return inputCSXLength;
		}

		internal static int GetOutputResetLengthEstimate()
		{
			return 20480;
		}
	}
}
